package de.webtech.faceplace.pages;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.hibernate.HibernateSessionManager;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.corelib.components.*;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.beaneditor.Validate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.google.inject.internal.cglib.proxy.Dispatcher;

import de.webtech.faceplace.entities.Comments;
import de.webtech.faceplace.entities.Messages;
import de.webtech.faceplace.entities.Person;

/**
 * Start page of application faceplace.
 */
public class Index
{	
	@Inject
	private Request request;
    
    @Inject
    private Session session;

    @Property
    private Messages currentMessage;
    
    @Property
    private Comments currentComment;
    
    @Property
    private String comment;
    
    @Property
    private String message;
   
    public Date getCurrentTime()
    {
        return new Date();
    }
    
    public Person getPerson(){
    	Person p = (Person) request.getSession(true).getAttribute("user");
    	
    	for(Object pe : session.createCriteria(Person.class).list()){
    		if(((Person) pe).id.equals(p.id)){
    			return (Person) pe;
    		}
    	}
    	return p;	
    }
    
    @CommitAfter
    public void onSubmitFromMessageForm()	{
		Person p = getPerson();
		Messages m = new Messages(message, p, 0);
		p.pushNewMessage(m);
		
		session.update(p);
    }
    
    @CommitAfter
    public void onSubmitFromCommentForm()	{
    	/*System.out.println(comment);
    	Person p = getPerson();
    	//currentMessage.pushComment(new Comments(comment, p));
		
		session.update(p);*/
    }

    
    public List<Messages> getMessages()
    {
    	List<Messages> a = new ArrayList<Messages>();
		Person p =  getPerson();
		
		if(p.messageList != null){
			for(Messages m:p.messageList)	{
				a.add(m);
			}
		}
		
		if(p.friendList != null){
			for(Person f : p.friendList)	{
				
				for(Object pe : session.createCriteria(Person.class).list()){
		    		if(((Person) pe).id.equals(f.id)){
		    			if(((Person) pe).messageList != null)	{
							System.out.println(((Person) pe).messageList.size());
							for(Messages m:((Person) pe).messageList)	{
								a.add(m);
							}
						}
		    		}
		    		}
				
			}
		}
		
		Collections.sort(a, new Comparator<Messages>() {
			  public int compare(Messages m1, Messages m2) {
			      return m2.getDate().compareTo(m1.getDate());
			  };
		});
		
		return a;
    }
    
    public List<Comments> getComments()
    {
    	return currentMessage.getComments();
    }
        
    /*
    //Demovorlagen von tapestry
    
    @Property
    @Inject
    @Symbol(SymbolConstants.TAPESTRY_VERSION)
    private String tapestryVersion;

    @InjectComponent
    private Zone zone;

    @Persist
    @Property
    private int clickCount;

    @Inject
    private AlertManager alertManager;

   

    void onActionFromIncrement()
    {
        alertManager.info("erhöhen geklickt");
        alertManager.info((String) request.getSession(false).getAttribute("user"));
        clickCount++;
    }

    Object onActionFromIncrementAjax()
    {
        clickCount++;

        alertManager.info("erhöhen (via Ajax) geklickt");

        return zone;
    }
    */
}
